﻿#include "ZFramelessWidget.h"
#include "ZRoundWidget.h"
#include "FramelessHelper.h"
#include <QGraphicsDropShadowEffect>
#include <QVBoxLayout>

ZFramelessWidget::ZFramelessWidget(QWidget *parent) :
    QWidget(parent),
    m_central_widget(NULL),
    m_round_effect(NULL)
{
    init();
}

ZFramelessWidget::~ZFramelessWidget()
{

}

QWidget *ZFramelessWidget::centralWidget()
{
    return m_central_widget;
}

void ZFramelessWidget::applyShadowEffect(ZShadowEffect *shadow_effect)
{
    if (!shadow_effect)
    {
        this->setGraphicsEffect(NULL);
        m_shadow_effect.reset(new ZShadowEffect);
    }
    else
    {
        m_shadow_effect.reset(shadow_effect);
    }

    // 内容显示窗口应用阴影
    int shadow_width = m_shadow_effect->getShadowWidth();
    QPoint offset = m_shadow_effect->getShadowOffset();

    QGraphicsDropShadowEffect *q_shadow_effect = new QGraphicsDropShadowEffect(this);
    q_shadow_effect->setBlurRadius(shadow_width);
    q_shadow_effect->setOffset(offset);
    q_shadow_effect->setColor(m_shadow_effect->getShadowColor());
    this->setGraphicsEffect(q_shadow_effect);

    // 调节边距以显示阴影
    // 偏移
    int adjusted_x_offset = qMin(shadow_width, offset.rx());
    int adjusted_y_offset = qMin(shadow_width, offset.ry());
    m_root_v_lay->setContentsMargins(shadow_width + adjusted_x_offset, shadow_width + adjusted_y_offset,
                                     shadow_width - adjusted_x_offset, shadow_width - adjusted_y_offset);
}

void ZFramelessWidget::applyRoundEffect(ZRoundEffect *round_effect)
{
    if (!round_effect)
    {
        m_round_effect.reset(new ZRoundEffect);
    }
    else
    {
        m_round_effect.reset(round_effect);
    }

    m_round_widget->setRound(m_round_effect->getXRadius(),
                             m_round_effect->getYRadius());
}

void ZFramelessWidget::applyBorderEffect(ZBorderEffect *border_effect)
{
    if (!border_effect)
    {
        m_border_effect.reset(new ZBorderEffect);
    }
    else
    {
        m_border_effect.reset(border_effect);
    }

    int pen_width = m_border_effect->getBorderWidth();
    m_vlay_round_widget->setContentsMargins(pen_width, pen_width, pen_width, pen_width);
    m_round_widget->setBorder(m_border_effect->getBorderWidth(),
                              m_border_effect->getBorderColor(),
                              m_border_effect->getPenStyle());
}

void ZFramelessWidget::init()
{
    m_border_effect.reset(new ZBorderEffect);
    m_shadow_effect.reset(new ZShadowEffect);
    m_round_effect.reset(new ZRoundEffect);

    // 去除窗口的系统默认边框.
    setWindowFlags(windowFlags() | Qt::FramelessWindowHint);

    // 注意此处设置透明之后，子控件也会变得背景透明（无背景）。
    // 一级子窗口需要自行设置背景色或重绘。
    setAttribute(Qt::WA_TranslucentBackground);

    m_root_v_lay = new QVBoxLayout;
    this->setLayout(m_root_v_lay);

    m_round_widget = new ZRoundWidget(this);
    m_root_v_lay->addWidget(m_round_widget);

    m_vlay_round_widget = new QVBoxLayout;

    int border_width = m_border_effect->getBorderWidth();
    m_vlay_round_widget->setContentsMargins(border_width, border_width, border_width, border_width);
    m_round_widget->setLayout(m_vlay_round_widget);

    m_central_widget = new QWidget(this);
    m_vlay_round_widget->addWidget(m_central_widget);

    m_frameless_helper = new FramelessHelper(this);
    m_frameless_helper->activateOn(this);
    m_frameless_helper->setWidgetMovable(true);
    m_frameless_helper->useRubberBandOnResize(true);
}
